package wellness.activities.data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import wellness.activities.beans.AtividadeBean;
import wellness.activities.beans.LoadBeans;
import wellness.activities.beans.UsuarioBean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import android.widget.Toast;

public class UsuarioData {
	private static String TABLE_NAME = "Usuario";

	private Context context;
	private SQLiteDatabase db;

	private SQLiteStatement insertStmt;
	private static final String INSERT = "insert into "
			+ TABLE_NAME
			+ "(USUA_NOME, USUA_SEXO, USUA_NASCIMENTO, USUA_ALTURA, USUA_PESO, USUA_PRO_ATIVIDADE, USUA_PRO_ATRATIVA) values (?, ?, ?, ?, ?, ?, ?)";

	public UsuarioData(Context context) {
		this.context = context;
	}

	public long insert(UsuarioBean u) {
		this.db = DataBase.getDB(this.context);
		this.insertStmt = this.db.compileStatement(INSERT);
		this.insertStmt.bindString(1, u.getNome());
		this.insertStmt.bindString(2, u.getSexo());
		this.insertStmt.bindString(3, new SimpleDateFormat("dd/MM/yyyy")
				.format(u.getDataNascimento().getTime()));
		this.insertStmt.bindDouble(4, u.getAltura());
		this.insertStmt.bindDouble(5, u.getPeso());
		this.insertStmt.bindLong(6, u.isPro_atividades() ? 1 : 0);
		this.insertStmt.bindLong(7, u.isPro_atrativa() ? 1 : 0);
		long i = this.insertStmt.executeInsert();
		DataBase.closeDB();
		return i;
	}

	public int resetar() {
		this.db = DataBase.getDB(this.context);
		int i = this.db.delete(TABLE_NAME, null, null);
		DataBase.closeDB();
		return i;
	}

	public int atualizar(Double peso, Double altura) {
		this.db = DataBase.getDB(this.context);
		ContentValues v = new ContentValues();
		v.put("USUA_PESO", peso);
		v.put("USUA_ALTURA", altura);
		int i = this.db.update(TABLE_NAME, v, null, null);
		DataBase.closeDB();
		return i;
	}

	public int atualizarProatividade(boolean atividade, boolean atrativa) {
		this.db = DataBase.getDB(this.context);
		ContentValues v = new ContentValues();
		v.put("USUA_PRO_ATIVIDADE", atividade ? 1 : 0);
		v.put("USUA_PRO_ATRATIVA", atrativa ? 1 : 0);
		int i = this.db.update(TABLE_NAME, v, null, null);
		DataBase.closeDB();
		return i;
	}

	public void selectAll() {
		this.db = DataBase.getDB(this.context);
		Cursor cursor = this.db
				.query(TABLE_NAME,
						new String[] { "USUA_ID, USUA_NOME, USUA_SEXO, USUA_NASCIMENTO, USUA_ALTURA, USUA_PESO, USUA_PRO_ATIVIDADE, USUA_PRO_ATRATIVA" },
						null, null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				UsuarioBean u = new UsuarioBean();
				u.setNome(cursor.getString(1));
				u.setSexo(cursor.getString(2));
				u.setDataNascimento(cursor.getString(3));
				u.setAltura(cursor.getDouble(4));
				u.setPeso(cursor.getDouble(5));
				u.setPro_atividades(cursor.getInt(6) == 1);
				u.setPro_atrativa(cursor.getInt(7) == 1);
				LoadBeans.setUsuario(u);
			} while (cursor.moveToNext());
		}
		if (cursor != null) {
			cursor.close();
		}
		DataBase.closeDB();
	}
}
